package com.syas.service.services;

import com.syas.base.service.services.EntityService;
import com.syas.common.extension.StringHelper;
import com.syas.dao.interfaces.IRateDao;
import com.syas.model.models.Rate;
import com.syas.service.interfaces.IRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author :lwl
 * @group :xinz
 * @Date : 2015-12-8 下午14:13:21
 * @Comments : 提成比例
 * @Version : 1.0.0
 */

@Service("RateService")
@Transactional()
class RateService extends EntityService<Integer, Rate, IRateDao> implements IRateService {

    @Autowired
    public RateService(@Qualifier("RateDao") IRateDao rateDao) {
        super(rateDao);
    }

    /**
     * @param station ：岗位名称
     * @param reachRate ：当月业绩目标达成率
     * @return ：Rate
     * @MethodName : getRate
     * @Description : 获得该岗位人员的提成比例
     */
    @Override
    public Rate getRate(String station,double reachRate) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where 1 =1 ");
        if(!StringHelper.isEmpty(station)){
            stringBuffer.append(" and station = '" + station + "'");
        }
        if(reachRate >= 0){
            stringBuffer.append(" and rate_low <= " + reachRate );
            stringBuffer.append(" and rate_high > " + reachRate);
        }
        return entityDao.getRate(stringBuffer.toString());
    }

    /**
     * @param reachRate ：当月业绩目标达成率
     * @return ：List<Rate>
     * @MethodName : getRateList
     * @Description : 获得团队经理及以上人员的提成比例的集合
     */
    @Override
    public List<Rate> getRateList(double reachRate) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(" select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where ");
        if(reachRate >= 0){
            stringBuffer.append(" rate_low <= " + reachRate );
            stringBuffer.append(" and rate_high > " + reachRate);
        }
        return entityDao.getRateListSQL(stringBuffer.toString());
    }

    /**
     * @return ：List<Rate>
     * @MethodName : getRateList
     * @Description : 获得团队经理及以上人员的提成比例的集合
     */
    public List<Rate> getRateList() {
        StringBuffer sb = new StringBuffer();
        sb.append("select tl.rate_low as rateLow,tl.rate_high as rateHigh,tl.rate as tlRate,um.rate as umRate,dm.rate as dmRate,cm.rate as cmRate,zm.rate as zmRate from ( ");
        sb.append("(select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where grade_code = \"TL\") tl,");
        sb.append("(select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where grade_code = \"UM\") um, ");
        sb.append("(select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where grade_code = \"DM\") dm, ");
        sb.append("(select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where grade_code = \"CM\") cm, ");
        sb.append("(select rate_code,rate ,grade_code ,rate_low,rate_high,station from syas.sy_rate where grade_code = \"ZM\") zm ");
        sb.append(" )where tl.rate_code = um.rate_code ");
        sb.append(" and tl.rate_code = dm.rate_code ");
        sb.append(" and tl.rate_code = cm.rate_code ");
        sb.append(" and tl.rate_code = zm.rate_code ");
        List<Rate> items = entityDao.getRateListSQL(sb.toString());
        return items;
    }
}
